package com.neardrug.controller;

import com.neardrug.common.Constants;
import com.neardrug.common.Result;
import com.neardrug.entity.example;
import com.neardrug.services.exampleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.openscience.cdk.depict.DepictionGenerator;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.silent.SilentChemObjectBuilder;
import org.openscience.cdk.smiles.SmilesParser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Base64;

/**
 * @author rui
 * @ClassName: exampleController
 * @create 2023-06-28 16:48
 */
@Api(tags = "实例")
@RestController
@RequestMapping("/example")
public class exampleController {

    @Resource
    private exampleService exampleService;

    @ApiOperation("检索")
    @GetMapping("/search")
    public Result doSearch(@RequestParam String smiles,
                           @RequestParam Integer method,
                           @RequestParam(defaultValue = "0.7") Double Threshold) {

        if (method == 1) {
            return Result.success(exampleService.searchSimil(smiles, Threshold));
        }
        if (method == 2) {
            return Result.success(exampleService.searchSS(smiles));
        }
        if (method == 3) {
            return Result.success(exampleService.searchSS(smiles));
        }
        return Result.error(Constants.CODE_500, "查询失败!");
    }

    @ApiOperation("根据专利号和实例号查找实例")
    @GetMapping("/{patentId}/{ComNum}")
    public Result getOne(@PathVariable String patentId,
                         @PathVariable int ComNum) {
        example one = exampleService.findOne(patentId, ComNum);
        return Result.success(one);

    }

    @GetMapping("/image/{smiles}")
    public Result show(@PathVariable String smiles) throws CDKException {
        IChemObjectBuilder bldr   = SilentChemObjectBuilder.getInstance();
        SmilesParser smipar = new SmilesParser(bldr);

        IAtomContainer mol = smipar.parseSmiles(smiles);

        DepictionGenerator dptgen = new DepictionGenerator();

        String s = dptgen.depict(mol).toSvgStr();

        String base64String = Base64.getEncoder().encodeToString(s.getBytes());
        return Result.success(base64String);
    }
}
